package com.bryanbibat.flashcards.spring;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.json.JSONArray;
import org.json.JSONException;

import com.google.appengine.api.datastore.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Deck {
  
  @PrimaryKey
  private String id;
  
  @Persistent
  private String title;
  
  @Persistent
  private Text data;
  
  @Persistent
  private Boolean readonly;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public Text getData() {
    return data;
  }

  public void setData(Text data) {
    this.data = data;
  }
  
  public String getJsonData() {
    return data==null ? null : data.getValue();
  }
  
  public void setJsonData(String jsonData) {
    this.data = new Text(jsonData);
  }

  public Boolean getReadonly() {
    return readonly;
  }

  public void setReadonly(Boolean readonly) {
    this.readonly = readonly;
  }

  @Override
  public String toString() {
    return "Deck [data=" + data + ", id=" + id + ", readonly=" + readonly
        + ", title=" + title + "]";
  }

  public int getDeckSize() {
    try {
      JSONArray dataArray = new JSONArray(getJsonData());
      return dataArray.length();
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return 0;
  }
  
}
